//
//  SteveZBarView.m
//  03绘制图表
//
//  Created by apple on 15-6-7.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "SteveZBarView.h"

@implementation SteveZBarView

- (void)drawRect:(CGRect)rect
{
    // 0. 构建数据
    NSArray* data = @[ @300, @150.65, @55.3, @507.7, @95.8, @700, @650.65 ];

    // 1. 获取图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // 2. 通过循环绘制每一个柱子
    for (int i = 0; i < data.count; i++) {

        CGFloat w = rect.size.width / (data.count * 2 - 1);
        CGFloat h = ([data[i] intValue] / 1000.0) * rect.size.height;
        CGFloat y = rect.size.height - h;
        CGFloat x = i * (w + w);

        // 创建一个绘制矩形的路径对象
        UIBezierPath* path = [UIBezierPath bezierPathWithRect:CGRectMake(x, y, w, h)];
        // 设置颜色
        [[UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1.0] set];

        // 把路径对象添加到图形上下文中
        CGContextAddPath(ctx, path.CGPath);

        // 渲染
        CGContextDrawPath(ctx, kCGPathFill);
    }
}

@end
